﻿using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.UI;
using AjaxControlToolkit;
using FutureOpen.Core.func.db;


/// <summary>
/// AutoComplete.asmx 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class UsrComplete : System.Web.Services.WebService
{
    private static string[] SerializedSchool = null;
    private static string[] SerializedTitle = null;
    private static string[] SerializedCompany = null;

    private static int CountSchool = 0;
    private static int CountTitle = 0;
    private static int CountCompany = 0;
    private const int Threshhold = 500;

    private static readonly Object SchoolLock = new Object();

    public UsrComplete() { }

    [WebMethod]
    public string[] GetTopSchoolList(string prefixText, int count)
    {   
        CountSchool++;
        if (CountSchool >= Threshhold || SerializedSchool == null || SerializedSchool.Length < count)
        {
            int i = 0;
            List<string> StrList = new List<string>();
            MySuggest ObjMySug = new MySuggest();
            List<FreqItem> FreqItems = ObjMySug.GetTopSchs(prefixText);
            foreach (FreqItem obj in FreqItems)
            {
                
                StrList.Add( new JavaScriptSerializer().Serialize( new Pair(obj.name, obj.freq.ToString() ) ) );
                i++;
                if(i>=count) break;
            }
            lock(SchoolLock)
            {
                SerializedSchool = StrList.ToArray();
                CountSchool = 0;
            }
        }
        return SerializedSchool;
    }

    [WebMethod]
    public string[] GetTopTitleList(string prefixText, int count)
    {
        CountTitle++;
        if (CountTitle >= Threshhold || SerializedTitle == null || SerializedTitle.Length< count)
        {
            int i = 0;
            List<string> StrList = new List<string>();
            MySuggest ObjMySug = new MySuggest();
            List<FreqItem> FreqItems = ObjMySug.GetTopTitles(prefixText);
            foreach (FreqItem obj in FreqItems)
            {
                StrList.Add(new JavaScriptSerializer().Serialize(new Pair(obj.name, obj.freq.ToString())));
                i++;
                if (i >= count) break;
            }
            lock (SchoolLock)
            {
                SerializedTitle = StrList.ToArray();
                CountTitle = 0;
            }
        }
        return SerializedTitle;
    }

    [WebMethod]
    public string[] GetTopCompanyList(string prefixText, int count)
    {
        CountCompany++;
        if (CountCompany >= Threshhold || SerializedCompany == null || SerializedCompany.Length < count)
        {
            int i = 0;
            List<string> StrList = new List<string>();
            MySuggest ObjMySug = new MySuggest();
            List<FreqItem> FreqItems = ObjMySug.GetTopCompanies(prefixText);
            foreach (FreqItem obj in FreqItems)
            {
                StrList.Add(new JavaScriptSerializer().Serialize(new Pair(obj.name, obj.freq.ToString())));
                i++;
                if (i >= count) break;
            }
            lock (SchoolLock)
            {
                SerializedCompany = StrList.ToArray();
                CountCompany = 0;
            }
        }
        return SerializedCompany;
    }

}